package com.lenovo.smarttraffic.ui.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MarkerOptionsCreator;
import com.lenovo.smarttraffic.R;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author Caleb_F
 * @description:
 * @date :2020-09-21 14:03
 */
public class MapTest extends BaseActivity {
    private MapView myMapTest;
    private Button myShowButton;
    private AMap aMap;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        myMapTest.onCreate(savedInstanceState);
        if(aMap==null){
            aMap = myMapTest.getMap();
        }
        myShowButton.setOnClickListener(v->{
            //经纬度的类，纬度在前，经度在后
            LatLng latLng = new LatLng(31.555, 118.5417);
            LatLngBounds.Builder builder = LatLngBounds.builder();
            for (int i = 0; i <6 ; i++) {
                float v1 = new Random().nextFloat();
                LatLng latLng1 = new LatLng(31.555 + v1, 118.5417 + v1);
                builder.include(latLng1);
               aMap.addMarker(new MarkerOptions().position(latLng1).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(getResources(), R.mipmap.marker_self))));
            }
            LatLngBounds build = builder.build();
            aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(build,200));

            //添加标记
            aMap.addMarker(new MarkerOptions().position(latLng).title("常州工程").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(), R.mipmap.marker_self))));
            //切换视角
            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));

        });



    }

    private void initView() {
        initToolBar("地图测试");
        myMapTest = (MapView) findViewById(R.id.my_map_test);
        myShowButton = (Button) findViewById(R.id.my_show_button);
    }

    @Override
    protected void onResume() {
        super.onResume();
        myMapTest.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myMapTest.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        myMapTest.onSaveInstanceState(outState);
    }


    @Override
    protected void onPause() {
        super.onPause();
        myMapTest.onPause();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_map_test;
    }
}
